package com.yf.serviceImpl;

import com.yf.bean.AnswerBean;
import com.yf.bean.ExamBean;
import com.yf.dao.AnswerDao;
import com.yf.dao.ExamDao;
import com.yf.entity.AnswerEntity;
import com.yf.entity.ExamEntity;
import com.yf.service.ExamService;
import com.yf.utils.Constant;
import com.yf.utils.Priority;
import com.yf.utils.UserContext;
import com.yf.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("ExamServiceImpl")
public class ExamServiceImpl implements ExamService {

    @Autowired
    ExamDao examDao;

    @Autowired
    AnswerDao answerDao;

//    private static final String ansSeq = String.valueOf((char) 18);

    @Override
    public Integer insert(Map<String, Object> params) {
        Map<String, Object> p = new HashMap<>();
        p.put("year", "2022");
        p.put("number", 0);
        p.put("question", "ques-test");
        p.put("answer", "");
        p.put("tag", "t1");
        p.put("type", "t1-1");
        p.put("subject", "cs");
        return examDao.insert(p);
    }

    // 返回新增答案ID
    @Override
    public Integer add408Question(ExamEntity entity) {
        final int userRole = UserContext.getUserRole();
        final Integer uid = UserContext.getUserId();

        ExamBean examBean = entity.getQuestion();
        Long pid = examBean.getId();
        if (pid != null && pid > 0) {
            Integer examUid = examDao.getExamUid(pid.intValue());
            if (uid.equals(examUid) || Priority.isAdmin(userRole)) {
                examDao.updateByIdV2(examBean);
            }
        } else if (!"0".equals(examBean.getType()) && examDao.existProblem(examBean) > 0) {
            // 根据 type year number 检索题目
            return -1;
        } else {
            // 新增题目
            examBean.setUid(uid);
            examDao.add408V2(examBean);
        }

        pid = examBean.getId();
        if (pid != null && pid > 0) {
            AnswerBean ans = entity.getAnswer();
            Integer aid = ans.getId();
            // 新增答案
            if (aid == null || aid == 0) {
                ans.setUid(uid);
                ans.setPid(examBean.getId().intValue());
                ans.setCreated(Utils.currentTime());
                return answerDao.addAnswer(ans);
            } else {
                // 更新答案
                Integer ansUid = answerDao.getAnsUid(aid);
                if (uid.equals(ansUid) || Priority.isAdmin(userRole)) {
                    return answerDao.updateAnswer(ans);
                }
            }
        }
        return -2;
    }

    @Override
    public ExamEntity getProblemByYear(Map<String, String> params) {
        ExamEntity ret = new ExamEntity();
        ExamBean examBean = examDao.getProblemByYearNumber(params);
        if (examBean != null) {
            ret.setQuestion(examBean);
            Map<String, Integer> p = new HashMap<>(2);
            p.put("pid", examBean.getId().intValue());
            p.put("uid", UserContext.getUserId());
            AnswerBean ans = answerDao.getAnsByPidUid(p);
            ret.setAnswer(ans);
        }
        return ret;
    }

    @Override
    public List<ExamBean> getTargetQus(List<Long> ids) {
        List<ExamBean> question = examDao.getTargetQus(ids);
        for (ExamBean qus: question) {
            List<String>  ansBuffer = new ArrayList<>(4);
            List<AnswerEntity> ans = answerDao.getAnsByPid(qus.getId().intValue());
            for (AnswerEntity a: ans) {
                ansBuffer.add(a.getAnsUser());
            }
            if (ansBuffer.size() > 0) {
                qus.setAnswer(String.join(Constant.ansSeq, ansBuffer));
            }
        }
        return question;
    }

    @Override
    public List<ExamBean> getFixedQus(List<Long> ids) {
        return examDao.getFixedQus(ids);
    }

    @Override
    public List<Integer> getQusIDByYear(Map<String, String> params) {
        return examDao.getQusIDByYear(params);
    }

    @Override
    public ExamBean getQusByYearNumber(Map<String, String> params) {
        ExamBean qus = examDao.getQusByYearNumber(params);
        if (qus != null && qus.getId() != null) {
            List<AnswerEntity> ans = answerDao.getAnsByPid(qus.getId().intValue());
            List<String>  ansBuffer = new ArrayList<>(4);
            for (AnswerEntity a: ans) {
                ansBuffer.add(a.getAnsUser());
            }
            qus.setAnswer(String.join(Constant.ansSeq, ansBuffer));
        }
        return qus;
    }

    @Override
    public List<AnswerBean> getAnswerByPid(Integer pid) {
        return answerDao.getOnlyAnswerByPid(pid);
    }

    @Override
    public Boolean existedQus(ExamBean bean) {
        return examDao.existProblem(bean) > 0;
    }

    @Override
    public List<ExamBean> getQusDescription(List<Integer> ids) {
        return examDao.getQusDescription(ids);
    }


    @Override
    public ExamBean getQuestionById(Long id) {
        return examDao.getQuestionById(id);
    }

    @Override
    public AnswerBean getUserAnswer(Integer pid) {
        final Integer userId = UserContext.getUserId();
        if (userId <= 0) {
            return null;
        }
        Map<String, Integer> params = new HashMap<>(2);
        params.put("pid", pid);
        params.put("uid", userId);
        return answerDao.getAnsByPidUid(params);
    }

    @Override
    public Integer userAddAnswer(String answer, Integer pid, Integer aid) {
        final Integer uid = UserContext.getUserId();
        if (uid <= 0) {
            return 0;
        }

        Map<String, Integer> params = new HashMap<>(2);
        params.put("pid", pid);
        params.put("uid", uid);
        final AnswerBean ansByPidUid = answerDao.getAnsByPidUid(params);
        if (ansByPidUid != null) {
            ansByPidUid.setAnswer(answer);
            answerDao.updateAnswer(ansByPidUid);
            return ansByPidUid.getId();
        } else {
            AnswerBean bean = new AnswerBean();
            bean.setAnswer(answer);
            if (aid == null || aid == 0) {
                bean.setPid(pid);
                bean.setUid(uid);
                answerDao.addAnswer(bean);
            } else {
                bean.setId(aid);
                answerDao.updateAnswer(bean);
            }
            return bean.getId();
        }
    }

    @Override
    public Boolean delAnswer(Integer aid) {
        final Integer uid = UserContext.getUserId();
        if (uid <= 0) {
            return false;
        }
        final Integer ansUid = answerDao.getAnsUid(aid);
        if (!ansUid.equals(uid)) {
            return false;
        }
        return answerDao.disableAnswer(aid) > 0;
    }

    @Override
    public Boolean userAddNote() {
        return null;
    }
}
